<template>
  <div class="search_container">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 8px">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>书籍类型</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="always">
      <!-- 搜索内容和导出区域 -->
      <el-row style="margin-bottom: 8px">
        <el-col :span="4">
          <el-button type="primary" @click="showAddDialog" size="large">添加分类</el-button>
        </el-col>
        <el-col :span="8" style="float: right">
          <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
          <el-button type="primary" class="el-icon-printer" size="large" @click="exportToExcel">
            导出Excel
          </el-button>
        </el-col>
        <!-- <el-col :span="6" style="float: right">
          <el-button type="primary" class="el-icon-printer" size="large" @click="downLoad">
            导出PDF
          </el-button>
        </el-col> -->
        <el-col :span="4" style="float: right">
          <el-button type="success" class="el-icon-full-screen" size="large" @click="fullScreen">
            全屏
          </el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
        :data="tableData"
        height="300"
        style="width: 100%"
        show-overflow-tooltip
        id="pdfDom"
        :default-sort="{ prop: 'typeId', order: 'ascending' }"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="TypeName" label="分类名"></el-table-column>
        <el-table-column prop="TypeContent" label="描述"></el-table-column>
        <el-table-column prop="CreateTime" label="创建时间" sortable></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <!-- 修改按钮 -->
            <el-button type="primary" @click="showEditDialog(row.TypeID)">
              <el-icon><EditPen /></el-icon>
            </el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" @click="removeUserById(row.TypeID)">
              <el-icon><Delete /></el-icon>
            </el-button>
          </template>
          <!-- 修改按钮 -->
        </el-table-column>
      </el-table>
      <!-- 分页查询区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 3, 4, 5]"
        :page-size="queryInfo.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 修改规则的对话框 -->
      <el-dialog title="修改分类" v-model="editDialogVisible" width="50%" @close="editDialogClosed">
        <el-form :model="editForm" ref="editFormRef" :rules="editFormRules" label-width="120px">
          <el-form-item label="分类名" prop="TypeName">
            <el-input v-model="editForm.TypeName"></el-input>
          </el-form-item>
          <el-form-item label="分类描述" prop="TypeContent">
            <el-input v-model="editForm.TypeContent" type="textarea"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateBookType">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 添加规则的对话框 -->

      <el-dialog title="添加分类" v-model="addDialogVisible" width="50%" @close="addDialogClosed">
        <el-form :model="addForm" ref="addFormRef" :rules="addFormRules" label-width="120px">
          <el-form-item label="分类名" prop="typeName">
            <el-input v-model="addForm.typeName"></el-input>
          </el-form-item>
          <el-form-item label="分类描述" prop="typeContent">
            <el-input v-model="addForm.typeContent" type="textarea"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addBookType">添加分类</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { getTypeList, getOneType, postOneType, delOneType, addType } from "../../api/api";
import * as XLSX from "xlsx";
// const visible = ref(false);
//类型列表
let tableData = ref();
//修改是否显示
const editDialogVisible = ref(false);
//修改表单
const editForm = reactive({
  TypeName: "",
  TypeContent: "",
  TypeId: "",
});
//修改规则
const editFormRules = reactive({
  TypeName: [{ required: true, message: "请输入分类名", trigger: "blur" }],
  TypeContent: [{ required: true, message: "请输入分类描述", trigger: "blur" }],
});
//增加可视化
const addDialogVisible = ref(false);
//增加表单
const addForm = reactive({
  typeName: "",
  typeContent: "",
});
//增加规则
const addFormRules = reactive({
  typeName: [{ required: true, message: "请输入分类名", trigger: "blur" }],
  typeContent: [{ required: true, message: "请输入分类描述", trigger: "blur" }],
});
//页面规则
const queryInfo = {
  pageNum: 1,
  pageSize: 5,
  condition: "",
  query: "",
};
//总数
const total = ref(0);
const title = "书籍类型";
const json_fields = {
  类别编号: "typeId",
  类别昵称: "typeName",
  类别概述: "typeContent",
};
const loading = ref(true);

onMounted(() => {
  getBookTypeList();
});
//导出excel
const exportToExcel = () => {
  //excel表格头部
  const headers = ["分类ID", "分类名", "描述"];
  // 转换表格数据为数组
  const ExcleData = tableData.value.map(item => [item.TypeID, item.TypeName, item.TypeContent]);
  // 创建一个工作簿
  const workbook = XLSX.utils.book_new();
  // 创建一个工作表
  const worksheet = XLSX.utils.aoa_to_sheet([headers, ...ExcleData]);

  // 将工作表添加到工作簿
  XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
  // 生成Excel文件
  XLSX.writeFile(workbook, "图书查询表格.xls");
};
//翻页
const handleSizeChange = val => {
  queryInfo.pageSize = val;
  getBookTypeList();
};
const handleCurrentChange = val => {
  queryInfo.pageNum = val;
  getBookTypeList();
};
//让修改公告的对话框可见,并从数据库中回显数据
const showEditDialog = id => {
  // // 让修改公告的对话框可见
  editDialogVisible.value = true;
  editForm.TypeId = id;
  getOneType(id)
    .then(res => {
      editForm.TypeName = res.data.TypeName;
      editForm.TypeContent = res.data.TypeContent;

      ElMessage({
        message: "获取信息成功",
        type: "success",
      });
    })
    .catch(err => {
      console.log(err, "err");
      ElMessage.error("获取信息失败");
    });
};
//监听修改对话框的关闭，一旦对话框关闭，就重置表单，回显数据
const editDialogClosed = () => {
  // $refs.editFormRef.resetFields();
  // editForm.checkList = ["南图", "北图", "教师之家"];
};
//删除公告
const removeUserById = id => {
  delOneType(id)
    .then(res => {
      console.log(res.data, "111");
      ElMessage({
        message: "删除类型成功",
        type: "success",
      });
    })
    .then(rew => {
      // // 防止删除出现数据显示错误
      getBookTypeList();
    })
    .catch(err => {
      console.log(err, "err");
      ElMessage.error("删除类型失败");
    });
};
//监听添加公告对话框的关闭，一旦对话框关闭，就重置表单
const addDialogClosed = () => {
  // $refs.addFormRef.resetFields();
};
//当用户点击发送新公告时，让添加对话框的visible改为true
const showAddDialog = () => {
  console.log(111111);
  addDialogVisible.value = true;
};
const getBookTypeList = () => {
  loading.value = true;
  getTypeList(queryInfo)
    .then(res => {
      tableData.value = res.data.records;
      console.log(tableData.value, "111");
      total.value = parseInt(res.data.total);
      ElMessage({
        message: "获取信息成功",
        type: "success",
      });
    })
    .catch(err => {
      console.log(err, "err");
      ElMessage.error("获取信息失败");
    });

  loading.value = false;
};
//增加类型
const addBookType = () => {
  addType(addForm)
    .then(res => {
      console.log(res.data, "111");
      ElMessage({
        message: "获取信息成功",
        type: "success",
      });
    })
    .then(rew => {
      // // 防止删除出现数据显示错误

      getBookTypeList();
    })
    .catch(err => {
      console.log(err, "err");
      ElMessage.error("获取信息失败");
    });

  addDialogVisible.value = false;
};
//更改类型
const updateBookType = () => {
  postOneType(editForm)
    .then(res => {
      console.log(res.data, "111");
      ElMessage({
        message: "更改信息成功",
        type: "success",
      });
    })
    .then(rew => {
      // // 防止删除出现数据显示错误

      getBookTypeList();
    })
    .catch(err => {
      console.log(err, "err");
      ElMessage.error("更改信息失败");
    });

  editDialogVisible.value = false;
};
const downLoad = () => {
  getPdf(title);
};
const fullScreen = () => {
  // Dom对象的一个属性: 可以用来判断当前是否为全屏模式(trueORfalse)
  let full = document.fullscreenElement;
  // 切换为全屏模式
  if (!full) {
    // 文档根节点的方法requestFullscreen实现全屏模式
    document.documentElement.requestFullscreen();
  } else {
    // 退出全屏模式
    document.exitFullscreen();
  }
};
</script>

<style></style>
